package com.eedi.framework.oauth2.controller.sys;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.oauth2.dal.dataobject.SysOAuth2ClientDO;
import com.eedi.framework.oauth2.service.SysOAuth2ClientService;
import com.eedi.framework.oauth2.controller.sys.vo.client.SysOAuth2ClientPageReq;
import com.eedi.framework.oauth2.controller.sys.vo.client.SysOAuth2ClientResp;
import com.eedi.framework.oauth2.controller.sys.vo.client.SysOAuth2ClientSaveReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;

//@Tag(name = "系统界面 - OAuth2 客户端")
//@RestController
//@RequestMapping("/oauth2-client")
//@Validated
public class SysOAuth2ClientController {

    @Resource
    private SysOAuth2ClientService sysOAuth2ClientService;

    @PostMapping("/create")
    @Operation(summary = "创建 OAuth2 客户端")
    @PreAuthorize("@ss.hasPermission('sys:oauth2-client:create')")
    public CommonResult<String> createOAuth2Client(@Valid @RequestBody SysOAuth2ClientSaveReq createReqVO) {
        return success(sysOAuth2ClientService.createOAuth2Client(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新 OAuth2 客户端")
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:update')")
    public CommonResult<Boolean> updateOAuth2Client(@Valid @RequestBody SysOAuth2ClientSaveReq updateReqVO) {
        sysOAuth2ClientService.updateOAuth2Client(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 OAuth2 客户端")
    @Parameter(name = "systemOauth2ClientId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:delete')")
    public CommonResult<Boolean> deleteOAuth2Client(@RequestParam("systemOauth2ClientId") String systemOauth2ClientId) {
        sysOAuth2ClientService.deleteOAuth2Client(systemOauth2ClientId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 OAuth2 客户端")
    @Parameter(name = "systemOauth2ClientId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:query')")
    public CommonResult<SysOAuth2ClientResp> getOAuth2Client(@RequestParam("systemOauth2ClientId") String systemOauth2ClientId) {
        SysOAuth2ClientDO client = sysOAuth2ClientService.getOAuth2Client(systemOauth2ClientId);
        return success(BeanUtils.toBean(client, SysOAuth2ClientResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得 OAuth2 客户端分页")
    @PreAuthorize("@ss.hasPermission('system:oauth2-client:query')")
    public CommonResult<PageResult<SysOAuth2ClientResp>> getOAuth2ClientPage(@Valid SysOAuth2ClientPageReq pageVO) {
        PageResult<SysOAuth2ClientDO> pageResult = sysOAuth2ClientService.getOAuth2ClientPage(pageVO);
        return success(BeanUtils.toBean(pageResult, SysOAuth2ClientResp.class));
    }

}
